<?php
class Site{
	public function getSiteInfo($url){
		$mid = Core::getInstance()->sys_load_class('MiddleResource');
		$data = $mid->_curl_get($url);
		$data = iconv('gb2312','utf-8',$data);
		$title = $this->getTitle($data);
		$a_tags = $this->getTagA($data);
		$this->getTagImg($data,$url);

		//echo $data;
	}

	private function getTitle(&$data){
		if(preg_match('/<title>([^<]+)<\/title>/i',$data,$matches)){
			return $matches[1];
		}
	}

	// Tag A Content
	private function getTagA(&$data){
		if(preg_match_all('/href="([^"]*)"[^>]*>([^<]*)<\/a>/i',$data,$matches)){
			//var_dump($matches);
		}
	}

	// Tag Image Content
	private function getTagImg(&$data,$url){
		if(preg_match_all('/src="([^"]+)"/i',$data,$matches)){
			foreach($matches[1] as &$v){
				if(strpos($v,'http://') === false){
					$v = $url.$v;
				}
				$this->downFile($v);
			}
		}
	}

	// Download The File
	private function downFile($url){
		$pos = strrpos($url,'/');
		$title = substr($url,$pos);
		file_put_contents(WEB_ROOT.'/images/'.$title,file_get_contents($url));
	}
}
